package com.future.mayi.dialog;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.widget.TextView;


import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareDialog extends BottomSheetDialog implements View.OnClickListener {

    private TextView tvWechat;
    private TextView tvMoments;
    private TextView tvCancel;
    private Activity mContext;

    public ShareDialog(@NonNull Activity context) {
        super(context, R.style.ShareDialog);
        this.mContext = context;
        setContentView(R.layout.dialog_share);
        initView();
        initListener();
    }

    private void initView() {
        tvWechat = (TextView) findViewById(R.id.tv_wechat);
        tvMoments = (TextView) findViewById(R.id.tv_moments);
        tvCancel = (TextView) findViewById(R.id.tv_cancel);

    }


    private void initListener() {
        tvWechat.setOnClickListener(this);
        tvMoments.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        setEnabled(false);
        boolean isClientValid;
        Platform platform;
        switch (view.getId()) {
            case R.id.tv_wechat:
                platform = ShareSDK.getPlatform(Wechat.NAME);
                isClientValid = platform.isClientValid();
                if (isClientValid) {
                    share(1, platform);
                } else {
                    new ToastUtils(mContext).show("手机未安装微信");
                    setEnabled(true);
                }
                break;
            case R.id.tv_moments:
                platform = ShareSDK.getPlatform(WechatMoments.NAME);
                isClientValid = platform.isClientValid();
                if (isClientValid) {
                    share(2, platform);
                } else {
                    new ToastUtils(mContext).show("手机未安装微信");
                    setEnabled(true);
                }
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
            default:
                break;
        }
    }

    private void setEnabled(boolean enabled) {
        tvWechat.setEnabled(enabled);
        tvMoments.setEnabled(enabled);
        tvCancel.setEnabled(enabled);
    }

    private String title, content, imgUrl, url;

    public void show(String title, String content, String imgUrl, String url) {
        this.title = title;
        this.content = content;
        this.imgUrl = imgUrl;
        this.url = url;
        show();
    }

    @Override
    public void show() {
        super.show();
        openOtherApp = false;
        setEnabled(true);
    }


    private void share(int type, Platform platform) {
        openOtherApp = true;
        Platform.ShareParams shareParams = new Platform.ShareParams();
        switch (type) {
            case 1:
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setImageUrl(imgUrl);
                shareParams.setTitle(title);
                shareParams.setText(content);
                shareParams.setUrl(url);
                platform.setPlatformActionListener(paListener);
                platform.share(shareParams);
                break;
            case 2:
                shareParams.setShareType(Platform.SHARE_WEBPAGE);
                shareParams.setTitle(title);
                shareParams.setText(content);
                shareParams.setImageUrl(imgUrl);
                shareParams.setUrl(url);
                platform.setPlatformActionListener(paListener);
                platform.share(shareParams);
                break;
            default:
                break;
        }

    }


    // 设置分享事件回调
    private PlatformActionListener paListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            new ToastUtils(mContext).show("分享成功");
            setEnabled(true);
            dismiss();
        }


        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            new ToastUtils(mContext).show("分享失败");
            setEnabled(true);
            dismiss();
        }

        @Override
        public void onCancel(Platform platform, int i) {
            new ToastUtils(mContext).show("分享取消");
            setEnabled(true);
            setEnabled(true);
            dismiss();
        }
    };

    private boolean openOtherApp = false;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (openOtherApp && hasFocus) {
            openOtherApp = false;
            dismiss();
        }
    }
}
